<?php  
	include("../includes/connect_db.php");
	include("../includes/core.php");

	$date = (isset($_POST["date"])) ? $_POST["date"] : "" ;
	$act = (isset($_POST["act"])) ? $_POST["act"] : "" ;

	switch ($act) {
		case 'monthviewrestaurant':
			if ($date != '') {
				$sql_menu = "SELECT a.id, a.nama FROM menu a";
				$query_menu = mysql_query($sql_menu);
				$count_menu = mysql_num_rows($query_menu);
				if ($count_menu > 0) {
					?>
					<thead>
						<tr>
							<th class="al_center" valign="middle">No</th>
							<th class="al_center" valign="middle">Menu Name</th>
							<?php
								$exdate = explode("-",$date);
								$jumHari = cal_days_in_month(CAL_GREGORIAN, $exdate[1], $exdate[0]);
								for ($m = 1; $m <= $jumHari ; $m++) { 
									?>
									<th class="al_center" valign="middle"><?php echo $m ?></th>
									<?php
								}
							?>
							<th class="al_center" valign="middle">Amount</th>
						</tr>
					</thead>
					<tbody>
						<?php
							$i = 1;
							$query_menu = mysql_query($sql_menu);
							while ($baris_menu = mysql_fetch_array($query_menu)) {
								?>
								<tr>
									<td class="al_center" valign="middle"><?php echo $i ?></td>
									<td class="al_left" valign="middle"><?php echo $baris_menu['nama'] ?></td>
										<?php
											$amount_menu = 0;
											for ($j=1; $j <= $jumHari ; $j++) { 
												$sql_date_menu = "SELECT MONTH(a.tanggal) AS month, SUM(b.jumlah) AS amount FROM resto a 
												LEFT JOIN resto_menu b ON b.idresto = a.id
												WHERE CONCAT(YEAR(a.tanggal),'-',MONTH(a.tanggal)) = '".$date."' AND DATE_FORMAT(a.tanggal, '%d') = '".$j."'
												AND a.id IN (SELECT idresto FROM nota WHERE is_paid = 1)
												AND b.idmenu = '".$baris_menu[id]."'";
												$query_date_menu = mysql_query($sql_date_menu);
												$date_menu = mysql_fetch_array($query_date_menu);
												if ($date_menu['amount'] < 1) {
													?>
													<td class="al_center" valign="middle">-</td>
													<?php
												}else{
													?>
													<td class="al_center" valign="middle">
														<?php echo $date_menu['amount'] ?>
													</td>
													<?php
													$amount_menu = $amount_menu+$date_menu['amount'];
												}
											}
										?>
										<td class="al_center" valign="middle"><?php echo $amount_menu ?></td>
									</td>
								</tr>
								<?php
								$i++;
								$total = $total+$amount_menu;
							}
						?>
						<tr>
							<td colspan="<?php echo $jumHari+2 ?>" class="al_right" valign="middle">Amount</td>
							<td class="al_center" valign="middle"><?php echo $total ?></td>
						</tr>
					</tbody>
					<?php
				}
			}
			break;

		case 'yearviewrestaurant':
			if ($date != '') {
				$sql_menu = "SELECT a.id, a.nama FROM menu a";
				$query_menu = mysql_query($sql_menu);
				$count_menu = mysql_num_rows($query_menu);
				if ($count_menu > 0) {
					?>
					<thead>
						<tr>
							<th class="al_center" valign="middle">No</th>
							<th class="al_center" valign="middle">Menu Name</th>
							<?php
								$month_name = array("Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
								for ($m = 1; $m <= 12 ; $m++) { 
									?>
									<th class="al_center" valign="middle"><?php echo $month_name[$m-1] ?></th>
									<?php
								}
							?>
							<th class="al_center" valign="middle">Amount</th>
						</tr>
					</thead>
					<tbody>
						<?php
							$i = 1;
							$query_menu = mysql_query($sql_menu);
							while ($baris_menu = mysql_fetch_array($query_menu)) {
								?>
								<tr>
									<td class="al_center" valign="middle"><?php echo $i ?></td>
									<td class="al_left" valign="middle"><?php echo $baris_menu['nama'] ?></td>
										<?php
											$amount_menu = 0;
											for ($j=1; $j <= 12 ; $j++) { 
												$sql_date_menu = "SELECT MONTH(a.tanggal) AS month, SUM(b.jumlah) AS amount FROM resto a 
												LEFT JOIN resto_menu b ON b.idresto = a.id
												WHERE b.idmenu = '".$baris_menu['id']."' AND YEAR(a.tanggal) = '".$date."' AND MONTH(a.tanggal) = '".$j."'";
												$query_date_menu = mysql_query($sql_date_menu);
												$date_menu = mysql_fetch_array($query_date_menu);
												if ($date_menu['amount'] < 1) {
													?>
													<td class="al_center" valign="middle">-</td>
													<?php
												}else{
													?>
													<td class="al_center" valign="middle">
														<?php echo $date_menu['amount'] ?>
													</td>
													<?php
													$amount_menu = $amount_menu+$date_menu['amount'];
												}
											}
										?>
										<td class="al_center" valign="middle"><?php echo $amount_menu ?></td>
									</td>
								</tr>
								<?php
								$i++;
								$total = $total+$amount_menu;
							}
						?>
						<tr>
							<td colspan="2" class="al_center" valign="middle">Amount</td>
							<?php
								for ($j=1; $j <= 12 ; $j++) { 
									$sql_date_amount = "SELECT MONTH(a.tanggal) AS month, SUM(b.jumlah) AS amount FROM resto a 
									LEFT JOIN resto_menu b ON b.idresto = a.id
									WHERE YEAR(a.tanggal) = '".$date."' AND MONTH(a.tanggal) = '".$j."'";
									$query_date_amount = mysql_query($sql_date_amount);
									$date_amount = mysql_fetch_array($query_date_amount);
									if ($date_amount['amount'] > 0) {
										?>
										<td class="al_center" valign="middle">
											<?php echo $date_amount['amount'] ?>
										</td>
										<?php
									}else{
										?>
										<td class="al_center" valign="middle">-</td>
										<?php
									}
								}
							?>
							<td class="al_center" valign="middle"><?php echo $total ?></td>
						</tr>
					</tbody>
					<?php
				}
			}
			break;
		
		default:
			# code...
			break;
	}
?>